;; -------- nox config via: LazyCat --------

;; (add-to-list 'nox-server-programs
;; 	     '((c++ mode c-mode) . (nox-cquery "cquery")))

;; (defclass nox-cquery (nox-lsp-server) ()
;;   :documentation "A custom class for cquery's C/C++ langserver.")

;; (cl-defmethod nox-initialization-options ((server nox-cquery))
;;   "Passes through required cquery initialization options"
;;   (let* ((root (car (project-roots (nox--project server))))
;;          (cache (expand-file-name ".cquery_cached_index/" root)))
;;     (list :cacheDirectory (file-name-as-directory cache)
;;           :progressReportFrequencyMs -1)))


;; -------- one-key compile --------
;; via: xuchunyang
;; (defun p1uxtar-g++-compile-and-run ()
;;   (interactive)
;;   (compile (format "g++ %s && ./a.out" (buffer-file-name))))

;; (define-key c++-mode-map (kbd "<f5>") #'p1uxtar-g++-compile-and-run)

;; via: sunlin7
(defun p1uxtar-compile-project-or-file ()
  "Compile current file just smart."
  (interactive)
  (cond ((and (boundp 'ede-minor-mode) ede-minor-mode (ede-current-project))
         (ede-compile-target))
        ((file-readable-p "Makefile")
         (compile compile-command))
        ((file-readable-p "makefile")
         (compile compile-command))
        ((string= "c-mode" major-mode)
         (let ((default-directory temporary-file-directory))
           (shell-command-on-region (point-min) (point-max)
                                    "gcc -g -O0 -x c -std=gnu11 -o a - && ./a")))
        ((string= "c++-mode" major-mode)
         (let ((default-directory temporary-file-directory))
           (shell-command-on-region (point-min) (point-max)
                                    "g++ -g -O0 -x c++ -std=c++11 -o a - && ./a")))
        ((call-interactively 'compile))))

;; -------- config using ccls via eglot --------
(when (eq system-type 'gnu/linux)
  (dolist (cc-hook (list
                    'c-mode-hook
                    'c++-mode-hook))
    (add-hook cc-hook (lambda ()
                        (add-to-list 'eglot-server-programs
                                     '((cc-mode) . ("/usr/bin/ccls")))
                        (eglot-ensure)
                        (c-turn-on-eldoc-mode) ;; c-eldoc
                        (electric-pair-mode 1)
                        (local-set-key (kbd "<f5>") #'p1uxtar-compile-project-or-file)))))

(provide '+cc)
